// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import App from './App'
import router from './router'
import store from './store/'
import './config/rem'
import { ToastPlugin } from 'vux'
import { ConfirmPlugin } from 'vux'

Vue.use(require('vue-wechat-title')) //微信动态改版头部

Vue.use(ConfirmPlugin)
Vue.use(ToastPlugin)

const history = window.sessionStorage

history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)


router.beforeEach(function (to, from, next) {
    store.dispatch('updateLoadingStatus', { isLoading: true })
    //控制滑动效果
    const toIndex = history.getItem(to.path)
    const fromIndex = history.getItem(from.path)

    if (toIndex) {
        if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
            store.dispatch('updateDirection', { direction: 'forward' })
        } else {
            store.dispatch('updateDirection', { direction: 'reverse' })
        }
    } else {
        ++historyCount
        history.setItem('count', historyCount)
        to.path !== '/' && history.setItem(to.path, historyCount)
        store.dispatch('updateDirection', { direction: 'forward' })
    }

    

    next() //一定要在这里加next，不然登录验证跳不过去
    //登录验证
    // if (to.matched.some(record => record.meta.requiresAuth)) {
    //     if (store.state.common.isLogin) {
    //         next() //如果isLogin为真，则无需登录
    //     } else {
    //         const user = window.localStorage.getItem('user');
    //         if (user) {
    //             next() //如果缓存存在，则无需登录
    //         } else {
    //             next({
    //                 path: '/login',
    //                 // query: { redirect: encodeURIComponent(to.path) } //否则登录，将目标路径作为参数，登录成功后直接跳转该路由，用decodeURIComponent()解码
    //             })
    //         }
    //     }
    // } else {
    //     next() // 确保一定要调用 next()
    // }
})

router.afterEach(function (to) {
    store.dispatch('updateLoadingStatus', { isLoading: false })
})


FastClick.attach(document.body)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app-box')
